package cn.org.mrliu.code.generator.web.element;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Input extends FormElement{
	private static final long serialVersionUID = 8618738161893502740L;
	private String type = TEXT;
	private String value;
	private String text;
	private boolean checked;
	private boolean readonly;
	private boolean disable;
	private boolean group;
	private Integer rows = 6;
	private Integer cols;
	private List<Input> children = new ArrayList<Input>();
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public boolean isReadonly() {
		return readonly;
	}

	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	public boolean isDisable() {
		return disable;
	}

	public void setDisable(boolean disable) {
		this.disable = disable;
	}

	public boolean isGroup() {
		return group;
	}

	public void setGroup(boolean group) {
		this.group = group;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	public Integer getCols() {
		return cols;
	}

	public void setCols(Integer cols) {
		this.cols = cols;
	}

	public List<Input> getChildren() {
		return children;
	}

	public void setChildren(List<Input> children) {
		this.children = children;
	}
	
	public String getTag() {
		if (type==TEXT) {
			return textTag();
		}else if (type==RADIO) {
			return radioTag();
		}
		return null;
	}
	private String textTag() {
		String tag = "";
		tag += "<label for='" + id + "'>" + label + "</label>\n";
		tag += "<input id='" + id + "' type='" + type + "' name='" + name + "' class='" + className + "' value='"
				+ value + "'";
		if (checked) {
			tag += " checked='checked'";
		}
		if (append != null) {
			tag += " " + append;
		}
		tag += "/>";
		if (text != null) {
			tag += " " + text;
		}
		tag += "\n";
		return tag;
	}

	private String _tag(Input input) {
		String tag = "";
		tag += "<input id='" + input.id + "' type='" + input.type + "' name='" + input.name + "' class='"
				+ input.className + "' value='" + input.value + "'";
		if (input.checked) {
			tag += " checked='checked'";
		}
		if (input.append != null) {
			tag += " " + input.append;
		}
		tag += "/>";
		if (input.text != null) {
			tag += " " + input.text;
		}
		tag += "\n";
		return tag;
	}

	private String radioTag() {
		String labelString = "<label for='" + id + "'>" + label + "</label>\n";
		String tag = "";
		tag += labelString;
		if (children != null && children.size() > 0) {
			for (Input radio : children) {
				tag += _tag(radio);
			}
		}
		tag += "\n";
		return tag;
	}
	

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}
}
